<?php

class Jd_task_jd_list_model extends MY_Model {





    public function __construct()
    {
        $this->modelename='Jd_task_jd_list_model';
        $this->tablename='task_jd_list';
        parent::__construct();
    }


    /**
     *
     * 商户任务添加添加具体单号
     * @param array $array
     * @return INT
     */
    public function  addtasklist($array=array()){
        $array['business_id']=$this->userId;
        $array['addtime']=time();
        $array['status']=0;
        $list=$array['list'];
        unset($array['list']);
        for ($x=1; $x<=$list; $x++) {
              $array['list']=$x;
              $r= $this->add($this->tablename,$array);
        }
        return  $r;
    }


    /**
     * 获取排序最小的子单任务
     */
    public function getminlitstake($taskid){
        $sql="select * from jd_task_jd_list  where  `status` =0  AND taskid=$taskid order by id asc limit 1 ";
        return $this->GetBySql($sql,0);
    }


    /**
     * 通过任务ID 获取具体子单号
     * @param array $idstr
     */
    public function getlistidbytask($idstr=array()){
        $sql='select * from jd_task_jd_list  where  `status` =0 AND  taskid in ('.implode($idstr,",").') ';
        return    $result=  $this->GetBySql($sql);
    }



    /**
     *
     * 多个任务被推送后更新状态
     * @param array $idstr
     */
    public  function  updateaccept($idstr=array()){
        $sql='update   jd_task_jd_list   set  `status` =1   where  `status` =0 AND  taskid in ('.implode($idstr,",").') ';
        return  $this->db->query($sql);
    }


    /**
     * 单个任务被推送后更新状态
     * @param $id
     * @return mixed
     */
    public  function  updateacceptone($id){
        $sql='update   jd_task_jd_list   set  `status` =1   where  `status` =0 AND  id= '.$id;
        return  $this->db->query($sql);
    }

    /**
     *
     * 子单任务加入任务池后更新状态
     * @param array $idstr
     */
    public  function  updatapool($idstr=array()){
        $sql='update   jd_task_jd_list   set  `status` =1   where  `status` =0 AND  id in ('.implode($idstr,",").') ';
        return  $this->db->query($sql);
    }


    /**
     * 更新任务实际用掉的钱
     */
    public function  updaterealmoney($id,$row){
        $real_price=$row['real_price'];
        $orderid=$row['orderid'];
        $awz=$row['awz'];
        $time=time();
        $sql="update   jd_task_jd_list   set real_price=$real_price, orderid='$orderid', awz='$awz', `update`=$time   where  id=$id";
      // echo $sql; exit;
        return  $this->db->query($sql);
    }


    /**
     * 计算所有
     */
    public function totalbunissmoney($star){

        $sql="select sum(a.price*a.`number`-a.real_price) as 'balance',a.business_id as 'user_id', b.money,b.gold,b.recommender as 'saler',b.username
from jd_task_jd_list  as a
left join jd_users as b on a.business_id=b.id 
 where addtime> $star  group by business_id";
        return    $result=  $this->GetBySql($sql);
    }


    /**
     * 获取这个月的数据
     */
    public function getmonthdata($user_id){
        $month=strtotime(date('Y-m-1'));
        $sql="select  count(1) as 'all'
from jd_task_jd_list  
 where hunter_id=$user_id AND addtime>=$month AND  `status`=2";
        return    $result=  $this->GetBySql($sql,0);
    }
    /**
     * 获取这个月的数据
     */
    public function getmonthdataall(){
        $month=strtotime(date('Y-m-1'));
        $sql="select  count(1) as 'all',`status`
from jd_task_jd_list  
 where addtime>=$month  group  by `status` ";
        return    $result=  $this->GetBySql($sql);
    }

    public function gettodayall(){
        $month=strtotime(date('Y-m-d'));
        $sql="select  count(1) as 'all',`status`
from jd_task_jd_list  
 where addtime>=$month  group  by `status`";
        return    $result=  $this->GetBySql($sql);
    }


    public function taskupdate($row)
    {
        $table=$this->tablename;
        $primary="taskid";
        $id=$row['id'];
        unset($row['id']);
        return $this->update($table, $primary, $id, $row,100000); // TODO: Change the autogenerated stub
    }


    /**
     * 废弃未被接取任务
     * @param $id
     * @return mixed
     */
    public function CancelStatus($id){
        $sql="update jd_task_jd_list  set `status`= 4   where taskid =$id   AND  `status`=0 ";
        return $r= $this->db->query($sql);
    }






}
